package com.data.platform.core.enums;

import com.data.platform.core.utils.RejectedExecutionHandlerUtils;

import java.util.Objects;
import java.util.concurrent.RejectedExecutionHandler;

public enum RejectedExecution implements IRejectedExecution{
    ABORT {
        @Override
        public RejectedExecutionHandler getRejectedExecutionHandler(String name) {
            return RejectedExecutionHandlerUtils.newAbort(name);
        }
    },
    DISCARD {
        @Override
        public RejectedExecutionHandler getRejectedExecutionHandler(String name) {
            return RejectedExecutionHandlerUtils.newDiscard(name);
        }
    },
    CALLER {
        @Override
        public RejectedExecutionHandler getRejectedExecutionHandler(String name) {
            return RejectedExecutionHandlerUtils.newCallerRuns(name);
        }
    },
    THREAD {
        @Override
        public RejectedExecutionHandler getRejectedExecutionHandler(String name) {
            return RejectedExecutionHandlerUtils.newThreadRun(name);
        }
    };

    @Override
    public RejectedExecutionHandler getRejectedExecutionHandler(String name) {
        return RejectedExecutionHandlerUtils.newAbort(name);
    }

    public static RejectedExecutionHandler of(String type, String name) {
        for (RejectedExecution rejectedExecution: values()) {
            if (Objects.equals(rejectedExecution.name(), type)) {
                return rejectedExecution.getRejectedExecutionHandler(name);
            }
        }

        return RejectedExecutionHandlerUtils.newAbort(name);
    }
}
